package com.yuxi.Dao.Impl;

import java.io.Serializable;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.yuxi.Config.UserTableStatic;
import com.yuxi.Dao.BaseDao;
import com.yuxi.Dao.UserInfoDao;
import com.yuxi.Entity.UserInfo;
import com.yuxi.Entity.UserView;
import com.yuxi.Request.Page;
import com.yuxi.Request.UserQuery;
@Repository
public  class UserInfoDaoImpl extends BaseDaoImpl<Serializable, Serializable> implements UserInfoDao {
	private Logger logger=LoggerFactory.getLogger(UserInfoDaoImpl.class);
	public UserInfoDaoImpl(){
		super(UserInfo.class);
	} 
	@Override
	public UserInfo getUser(Integer userId) {		
		return (UserInfo) super.get(userId);		
	}

	@Override
	public UserInfo addUser(UserInfo u) {		
		return  (UserInfo) super.addAndReturn(u);
	}

	@Override
	public void deleteUser(UserInfo u) {
		super.delete(u);
	}

	@Override
	public void modifly(UserInfo u) {	
		UserTableStatic.getAllByDb();
		super.update(u);
	}

	@Override
	public UserInfo login(String userAccount,String pwd) {
		try{
			UserInfo u=(UserInfo)super.find("from UserInfo where userAccount=? and userPassword=?", userAccount,pwd).get(0);
			logger.info("用户名:"+u.getUserName()+" 用户工号:"+u.getUserAccount()+"登陆成功！");
			return u;
		}catch(Exception e){
			logger.error("用户登录失败！！！");
			return null;
		}	
	}
	
	@Override
	public List<UserInfo> getUserByRole(Object[] RoleId) {
		StringBuilder inStr=new StringBuilder(RoleId[0].toString());		
		for(int i=1;i<RoleId.length;i++){			
			inStr.append(","+RoleId[i]);
		}		
		return (List<UserInfo>)super.find("select 	new UserInfo(u.userId, u.userName, u.userPassword, u.userAccount, u.userHeadPortait, u.phone, u.idNumber, u.createUserid, u.createTimg) from PowerUser p,UserInfo u where p.roleId in("+inStr.toString()+") and p.userId=u.userId");
	}
	@Override
	public Page<UserView> getCreateUserList(UserQuery userQuery, int userId,Page<UserView> p) {
		//String hql="select DISTINCT new com.yuxi.Entity.UserView(u.userId, u.userName, u.userAccount, u.userHeadPortait, u.phone, u.idNumber, u.createUserid, u.createTimg, pU.roleId) from UserInfo u,PowerUser pU where  u.userId=pU.userId and u.createTimg between ? and ? and (u.userAccount like ? or u.userName like ?) ";
		String hql="select  new com.yuxi.Entity.UserView(u.userId, u.userName, u.userAccount, u.userHeadPortait, u.phone, u.idNumber, u.createUserid, u.createTimg,u.enable, pU.roleId) from UserInfo u,PowerUser pU where  u.userId=pU.userId and u.createUserid=? and u.createTimg between ? and ? and (u.userAccount like ? or u.userName like ?) ";
		String shql="from UserInfo u,PowerUser pU where u.userId=pU.userId and u.createUserid=? and u.createTimg between ? and ? and (u.userAccount like ? or u.userName like ?)";
		return (Page<UserView>)super.splitPageQuerySlect(hql, p,shql,userId,userQuery.getStartTimestamp(),userQuery.getEndTimestamp(),"%"+userQuery.getUserAccountOrName()+"%","%"+userQuery.getUserAccountOrName()+"%");
	}
	@Override
	public Page<UserView> getUserList(String userAccountOrName) {		
		return (Page<UserView>) super.find("from UserInfo where  userAccount like ? or userName like ?", userAccountOrName);
	}
	@Override
	public List<UserInfo> getUserListByUserAccountOrName(String userAccountOrName) {
		// TODO Auto-generated method stub
		return (List<UserInfo>) super.find("from UserInfo where  userAccount like ? or userName like ?", userAccountOrName,userAccountOrName);
	}
	@Override
	public UserView getUserViewById(Integer id) {
		// TODO Auto-generated method stub
		String hql="select  new com.yuxi.Entity.UserView(u.userId, u.userName, u.userAccount, u.userHeadPortait, u.phone, u.idNumber, u.createUserid, u.createTimg,u.enable,pU.roleId) from UserInfo u,PowerUser pU  where  u.userId=pU.userId  and u.userId=? ";
		return (UserView) super.find(hql, id).get(0);
	}
	@Override
	public UserView getUserViewByUserId(Integer id) {
		// TODO Auto-generated method stub
		String hql="select  new com.yuxi.Entity.UserView(u.userId, u.userName, u.userAccount, u.userHeadPortait, u.phone, u.idNumber, u.createUserid, u.createTimg,u.enable,pU.roleId, uP.positionId,uP.positionName) from UserInfo u,UserPosition uP,PowerUser pU  where  u.userId=pU.userId and u.userId=uP.userId and u.userId=? ";
		List rs=super.find(hql, id);
		if(rs.size()>1&&rs!=null){
			return (UserView)rs.get(0);
		}else{
			return null;
		}		
	}
	@Override
	public  List<UserInfo> getPrisonHeader(int pId) {
		
		String hql="select new UserInfo(userInfoTalbe.userId, userInfoTalbe.userName) from"
				+ " UserInfo as userInfoTalbe,PowerUser pUTable,PrisonRole pRTable"
				+ " where userInfoTalbe.userId=pUTable.userId and "
				+ "pUTable.roleId=pRTable.id and pRTable.prisonStructure.id=? and pRTable.type=2"
				+ "";
		return (List<UserInfo>) super.find(hql,pId);
	}
	@Override
	public List<UserInfo> getPrisonViceHeader(int pId) {
		String hql="select new UserInfo(userInfoTalbe.userId, userInfoTalbe.userName) from"
				+ " UserInfo as userInfoTalbe,PowerUser pUTable,PrisonRole pRTable"
				+ " where userInfoTalbe.userId=pUTable.userId and "
				+ "pUTable.roleId=pRTable.id and pRTable.prisonStructure.id=? and pRTable.type=1"
				+ "";
		return (List<UserInfo>) super.find(hql,pId);
	}
//	@Override
//	public Page<UserView> getStruectureUserList(UserQuery userQuery,int structureId,  Page<UserView> p) {
//		String hql="select  new com.yuxi.Entity.UserView(u.userId, u.userName, u.userAccount, u.userHeadPortait, u.phone, u.idNumber, u.createUserid, u.createTimg,u.enable,pU.roleId,uS.structureId,uP.structureName) from UserInfo u,UserStructure uS where  u.userId=uS.userId  and uS.structureId=? and u.createTimg between ? and ? and (u.userAccount like ? or u.userName like ?) ";
//		String shql="from UserInfo u,UserStructure uS where u.userId=uS.userId and uS.structureId=? and u.createTimg between ? and ? and (u.userAccount like ? or u.userName like ?)";
//		return (Page<UserView>)super.splitPageQuerySlect(hql, p,shql,structureId,userQuery.getStartTimestamp(),userQuery.getEndTimestamp(),"%"+userQuery.getUserAccountOrName()+"%","%"+userQuery.getUserAccountOrName()+"%");
//
//	}
	@Override
	public Page<UserView> getStruectureUserList(UserQuery userQuery,int positionId,  Page<UserView> p) {
		String hql="select  new com.yuxi.Entity.UserView(u.userId, u.userName, u.userAccount, u.userHeadPortait, u.phone, u.idNumber, u.createUserid, u.createTimg,u.enable,pU.roleId,uP.positionId,uP.positionName) from UserInfo u,UserPosition uP,PowerUser pU where  u.userId=uP.userId and u.userId=pU.userId and uP.positionId=? and u.createTimg between ? and ? and (u.userAccount like ? or u.userName like ?) ";
		String shql="from UserInfo u,UserPosition uP where u.userId=uP.userId and uP.positionId=? and u.createTimg between ? and ? and (u.userAccount like ? or u.userName like ?)";
		return (Page<UserView>)super.splitPageQuerySlect(hql, p,shql,positionId,userQuery.getStartTimestamp(),userQuery.getEndTimestamp(),"%"+userQuery.getUserAccountOrName()+"%","%"+userQuery.getUserAccountOrName()+"%");

	}
	@Override
	public Page<UserView> getStruectureAllUser(UserQuery userQuery,Integer structureId,  Page<UserView> p) {
		String str=" ";
		if(structureId!=null){
			if (structureId==1) {
				str=" ";
			}else{
				str=" and uS.structureId="+structureId;
			}
			
		}
		String hql="select distinct new com.yuxi.Entity.UserView(u.userId, u.userName, u.userAccount, u.userHeadPortait, u.phone, u.idNumber, u.createUserid, u.createTimg,u.enable,pU.roleId,Rt.roleName,uP.positionId,uP.positionName,uS.structureId,uS.structureName) from UserInfo u,UserPosition uP,UserStructure uS,PowerUser pU,Role Rt where  u.userId=uP.userId and uS.userId=u.userId and u.userId=pU.userId and pU.roleId=Rt.id "
				+ str
				+ " and u.createTimg between ? and ? and (u.userAccount like ? or u.userName like ?) ";
		String shql=" u.userId )from UserInfo u,UserPosition uP,UserStructure uS,PowerUser pU where u.userId=uP.userId and u.userId=uS.userId and u.userId=pU.userId "
				+str
				+ " and u.createTimg between ? and ? and (u.userAccount like ? or u.userName like ?)";
		
		return (Page<UserView>)super.splitPageQuerySelect(hql, p,shql,userQuery.getStartTimestamp(),userQuery.getEndTimestamp(),"%"+userQuery.getUserAccountOrName()+"%","%"+userQuery.getUserAccountOrName()+"%");

	}
	@Override
	public List<UserInfo> getALl() {		
		return super.find("from UserInfo");
	}
}
